{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Is Q mxn or nxn?  It is both!  This is useful! "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×4 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:\n",
       " -0.36166    0.916348   -0.135025   -0.106193 \n",
       " -0.875369  -0.396235   -0.176529   -0.21346  \n",
       " -0.216      0.0556349   0.974526   -0.0234073\n",
       " -0.237226   0.0144527  -0.0300856   0.970881 "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using LinearAlgebra\n",
    "A = rand(4,2)\n",
    "Q,R = qr(A)\n",
    "Q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## It looks 4x4 !!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 4)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "size(Q)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## but wait! I can multiply by a vector of size 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4-element Array{Float64,1}:\n",
       "  1.4710364530489386 \n",
       " -1.6678387032016588 \n",
       " -0.10473038358204545\n",
       " -0.2083203318415276 "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q * [1,2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## what about size 3? !! (answer: no)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "DimensionMismatch",
     "evalue": "DimensionMismatch(\"vector must have length either 4 or 2\")",
     "output_type": "error",
     "traceback": [
      "DimensionMismatch(\"vector must have length either 4 or 2\")",
      "",
      "Stacktrace:",
      " [1] *(::LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}, ::Array{Int64,1}) at /Users/sabae/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.3/LinearAlgebra/src/qr.jl:563",
      " [2] top-level scope at In[16]:1"
     ]
    }
   ],
   "source": [
    "Q * [1,2,3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## what about size 4? (answer: yes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4-element Array{Float64,1}:\n",
       "  0.6411891565050274\n",
       " -3.051264992118778 \n",
       "  2.7252182423170517\n",
       "  3.5849468538868363"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q * [1,2,3,4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What's going on??"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q is not stored as elements, it is stored in a more compact form known as a [WY representation](https://www.researchgate.net/publication/23844885_A_Storage-Efficient_WY_Representation_for_Products_of_Householder_Transformations) which we do not cover in 18.06.\n",
    "This form not only saves memory, but allows us to complete the tall-skinny mxn Q into a full square orthogonal Q."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The \"extra\" vectors are an orthonormal set of vectors that are orthogonal to the column space of A.  This is associated with the left nullspace of A."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×4 Array{Float64,2}:\n",
       " 0.0486726  0.422254  0.0384858  0.324084\n",
       " 0.700173   0.479445  0.570882   0.340941\n",
       " 0.868803   0.791772  0.5067     0.294249\n",
       " 0.550927   0.680756  0.133569   0.127481"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = rand(4,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×3 Array{Float64,2}:\n",
       " 0.422254  0.0384858  0.324084\n",
       " 0.479445  0.570882   0.340941\n",
       " 0.791772  0.5067     0.294249\n",
       " 0.680756  0.133569   0.127481"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[:,2:end]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×3 Array{Float64,2}:\n",
       " 0.422254  0.0384858  0.324084\n",
       " 0.479445  0.570882   0.340941\n",
       " 0.791772  0.5067     0.294249\n",
       " 0.680756  0.133569   0.127481"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[:,2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×3 Array{Float64,2}:\n",
       " 0.422254  0.0384858  0.324084\n",
       " 0.479445  0.570882   0.340941\n",
       " 0.791772  0.5067     0.294249\n",
       " 0.680756  0.133569   0.127481"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[:,[2,3,4]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Six Cases (kind of)\n",
    "1. Square (rank=n rank<n)\n",
    "2. Tall skinny (rank=n rank<n)\n",
    "3. short wide  (rank=m rank<m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearAlgebra.QRCompactWY{Float64,Array{Float64,2}}\n",
       "Q factor:\n",
       "3×3 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:\n",
       " -0.688382   0.256761  -0.678383\n",
       " -0.536173  -0.810009   0.237495\n",
       " -0.488517   0.527218   0.695264\n",
       "R factor:\n",
       "3×3 Array{Float64,2}:\n",
       " -1.25521  -0.756215   -1.14882 \n",
       "  0.0       0.0596418  -0.293094\n",
       "  0.0       0.0        -0.393284"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using LinearAlgebra\n",
    "A = rand(3,3) # surely full rank\n",
    "Q,R = qr(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Q is square orthogonal , R is square, non-singular  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Float64,1}:\n",
       " 0.8640659111629196\n",
       " 0.6730109100970267\n",
       " 0.6131928969286227"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v = A[:,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.2552127759018195"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "norm(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Float64,1}:\n",
       " 0.6883820239497828\n",
       " 0.5361727692848695\n",
       " 0.4885170934370617"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v/norm(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "## what if it is square not full rank?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×3 Array{Int64,2}:\n",
       " 1  2  3\n",
       " 2  4  6\n",
       " 1  1  1"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [ 1 2 3\n",
    "      2 4 6\n",
    "      1 1 1 ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rank(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Float64,1}:\n",
       " 8.519782928662694    \n",
       " 0.6428832308185806   \n",
       " 7.528243337817958e-16"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "svdvals(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearAlgebra.QRCompactWY{Float64,Array{Float64,2}}\n",
       "Q factor:\n",
       "3×3 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:\n",
       " -0.408248  -0.182574  -0.894427   \n",
       " -0.816497  -0.365148   0.447214   \n",
       " -0.408248   0.912871  -8.88178e-16\n",
       "R factor:\n",
       "3×3 Array{Float64,2}:\n",
       " -2.44949  -4.49073   -6.53197    \n",
       "  0.0      -0.912871  -1.82574    \n",
       "  0.0       0.0        1.55431e-15"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q,R = qr(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×3 Array{Float64,2}:\n",
       "  1.0          -5.55112e-17  1.96064e-16\n",
       " -5.55112e-17   1.0          4.10453e-16\n",
       "  1.96064e-16   4.10453e-16  1.0        "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q'Q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## if A is square and A = Q*R  if A is singular, Q is still orthogonal, but R may not be invertible"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## (AB)⁻¹  = B⁻¹ * A⁻¹"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hence if the product of two matrices is not invertible then at least one of the two is not invertible"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## An orthogonal matrix has an inverse , i.e. Qᵀ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearAlgebra.QRCompactWY{Float64,Array{Float64,2}}\n",
       "Q factor:\n",
       "3×3 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:\n",
       " 1.0  0.0  0.0\n",
       " 0.0  1.0  0.0\n",
       " 0.0  0.0  1.0\n",
       "R factor:\n",
       "3×3 Array{Float64,2}:\n",
       " 0.0  0.0  0.0\n",
       " 0.0  0.0  0.0\n",
       " 0.0  0.0  0.0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q,R = qr(zeros(3,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tall Skinny"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6×3 Array{Float64,2}:\n",
       " 0.253612  0.904386  0.54806 \n",
       " 0.880715  0.814656  0.227191\n",
       " 0.713426  0.799345  0.3588  \n",
       " 0.502016  0.225784  0.623123\n",
       " 0.64694   0.304476  0.987695\n",
       " 0.481788  0.806257  0.262103"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = rand(6,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Float64,1}:\n",
       " 2.471516297197189 \n",
       " 0.8480652908171992\n",
       " 0.5015253422131328"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "svdvals(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rank(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "Q,R = qr(A);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×3 Array{Float64,2}:\n",
       " -1.50054  -1.47672   -1.11502  \n",
       "  0.0       0.856476  -0.0306984\n",
       "  0.0       0.0        0.817942 "
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6×3 Array{Float64,2}:\n",
       " -0.169013   0.764528    0.468342\n",
       " -0.586931  -0.0608065  -0.52463 \n",
       " -0.475446   0.113538   -0.205207\n",
       " -0.334556  -0.313218    0.293994\n",
       " -0.431137  -0.387863    0.605252\n",
       " -0.321076   0.38777    -0.102697"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q = Q[:,1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q*R ≈ A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×3 Array{Float64,2}:\n",
       " 1.0           3.46945e-16   9.71445e-17\n",
       " 3.46945e-16   1.0          -2.35922e-16\n",
       " 9.71445e-17  -2.35922e-16   1.0        "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q'Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6×6 Array{Float64,2}:\n",
       "  0.832412   -0.192995   0.0710531  -0.0452299   0.0598008   0.30263  \n",
       " -0.192995    0.623423   0.379808    0.0611691  -0.040901    0.218749 \n",
       "  0.0710531   0.379808   0.281049    0.0631715   0.0367432   0.217755 \n",
       " -0.0452299   0.0611691  0.0631715   0.296466    0.443666   -0.044231 \n",
       "  0.0598008  -0.040901   0.0367432   0.443666    0.702647   -0.0741314\n",
       "  0.30263     0.218749   0.217755   -0.044231   -0.0741314   0.264003 "
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q*Q'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## if A is tall-skinny, Q matches A (well julia has something here, but don't worry) and R is invertible square"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What is this good for?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "this is how least squares really happens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6-element Array{Float64,1}:\n",
       " 0.0478792078122805 \n",
       " 0.27000278307530534\n",
       " 0.31372385185711127\n",
       " 0.8684428375752447 \n",
       " 0.6468058271127577 \n",
       " 0.4412365841937491 "
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = rand(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Float64,1}:\n",
       "  0.6267259740756708 \n",
       " -0.32726590196323513\n",
       "  0.5108893852341747 "
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A\\b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Float64,1}:\n",
       "  0.6267259740756709\n",
       " -0.3272659019632352\n",
       "  0.5108893852341747"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R \\( Q'b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4×4 Array{Float64,2}:\n",
       " 0.0493362   0.574836  0.88775   0.470262\n",
       " 0.00808782  0.711997  0.607727  0.476851\n",
       " 0.917347    0.838914  0.104311  0.97497 \n",
       " 0.723469    0.645267  0.240534  0.305889"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = rand(4,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6×3 Array{Int64,2}:\n",
       " 1  2  3\n",
       " 2  4  6\n",
       " 3  6  9\n",
       " 1  1  1\n",
       " 1  1  1\n",
       " 1  1  1"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [ 1 2 3;2 4 6; 3 6 9; 1 1 1; 1 1 1; 1 1 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rank(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Float64,1}:\n",
       " 14.274567269631934  \n",
       "  1.1120832993767427 \n",
       "  3.2684080872602e-16"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "svdvals(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6×3 Array{Float64,2}:\n",
       " -0.242536  -0.112272  -0.512343   \n",
       " -0.485071  -0.224544   0.783521   \n",
       " -0.727607  -0.336817  -0.351567   \n",
       " -0.242536   0.523937   1.11022e-16\n",
       " -0.242536   0.523937   1.11022e-16\n",
       " -0.242536   0.523937   1.11022e-16"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q,R = qr(A)\n",
    "Q = Q[:,1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q*R ≈ A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6×3 Array{Float64,2}:\n",
       " -0.242536  -0.112272  -0.512343   \n",
       " -0.485071  -0.224544   0.783521   \n",
       " -0.727607  -0.336817  -0.351567   \n",
       " -0.242536   0.523937   1.11022e-16\n",
       " -0.242536   0.523937   1.11022e-16\n",
       " -0.242536   0.523937   1.11022e-16"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×3 Array{Float64,2}:\n",
       " -4.12311  -7.5186   -10.9141     \n",
       "  0.0      -1.57181   -3.14362    \n",
       "  0.0       0.0       -4.00297e-16"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×3 Array{Float64,2}:\n",
       "  1.0          -5.55112e-17   8.57529e-17\n",
       " -5.55112e-17   1.0          -1.9783e-17 \n",
       "  8.57529e-17  -1.9783e-17    1.0        "
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q'Q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## summary of the four cases so far\n",
    "\n",
    "for square and tall-skinny A = QR where the shape of Q is the shape of A and R is invertible or not, depending on whether A isfull rank or not"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## now for the short wide case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×6 Array{Float64,2}:\n",
       " 0.864337  0.974244  0.508762  0.664655  0.556382  0.547616\n",
       " 0.378073  0.491839  0.518706  0.902492  0.584839  0.389362\n",
       " 0.555097  0.702286  0.426364  0.582923  0.553117  0.325505"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = rand(3,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rank(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×6 Array{Float64,2}:\n",
       " -1.0946  -1.29532    -0.797116  -1.13217   -0.921841  -0.731974 \n",
       "  0.0     -0.0799796  -0.221097  -0.428477  -0.300855  -0.0683788\n",
       "  0.0      0.0        -0.159382  -0.361468  -0.131273  -0.130301 "
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q,R = qr(A);\n",
    "R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×3 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:\n",
       " -0.789637   0.60756   -0.0856996\n",
       " -0.345398  -0.555592  -0.756318 \n",
       " -0.507123  -0.567616   0.648566 "
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×3 Array{Float64,2}:\n",
       " 1.0          2.22045e-16  1.66533e-16\n",
       " 2.22045e-16  1.0          1.11022e-16\n",
       " 1.66533e-16  1.11022e-16  1.0        "
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q'Q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A = Q * [R  R₂] where R is non-singular upper triangular (full row rank)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×6 Adjoint{Int64,Array{Int64,2}}:\n",
       " 1  2  3  1  1  1\n",
       " 2  4  6  1  1  1\n",
       " 3  6  9  1  1  1"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A =[ 1 2 3;2 4 6; 3 6 9; 1 1 1; 1 1 1; 1 1 1]'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rank(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "Q,R = qr(A); "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×6 Array{Float64,2}:\n",
       " -3.74166  -7.48331      -11.225        -1.60357   -1.60357   -1.60357 \n",
       "  0.0       1.98603e-15    3.97205e-15   0.622821   0.622821   0.622821\n",
       "  0.0       0.0           -3.9443e-31   -0.201656  -0.201656  -0.201656"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×3 LinearAlgebra.QRCompactWYQ{Float64,Array{Float64,2}}:\n",
       " -0.267261   0.956183    0.119523\n",
       " -0.534522  -0.0439019  -0.844013\n",
       " -0.801784  -0.28946     0.522835"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A = Q * [R  R₂] where R is singular upper triangular (not full row rank)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## For a matrix Q to satisfy Q'Q =I then Q must have no more columns than rows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example if such a Q existed for 3x6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "then you would have 6 linearly independent vectors in R³"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Qx=0  # ask is x=0 the only solution?    Q'Qx=0=Ix  so x=0 so the columns are linearly indpendent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.3.0",
   "language": "julia",
   "name": "julia-1.3"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.3.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
